import * as reportsService from '../services/reports';
export default {
    namespace: 'reports',
    state: {
        allUsersList: [],
        List: [],
        total: 0,
        page: 1,
        pageSize: 5,
        info: {
            content: '<p><br/></p>',

        }
    },
    reducers: {
        setData(state, {
            payload,
        }) {
            return {
                ...state,
                allUsersList: payload,
            };
        },
        setReports(state, {
            payload:{
                list,
                total,
                page,
            }
        }) {
            return {
                ...state,
                list,
                total,
                page,
            };
        },
        setInfo(state, {payload}) {
            return {...state,info: payload};
        }
    },
    effects: {
        * getAllUsers({
            _
        }, {
            call,
            put,
        }) {
            const res = yield call(reportsService.fetch,);
            console.log(res);
            if (res && res.state === 'success') {
                yield put({
                    type: 'setData',
                    payload: res.data,
                });
            } else {
                yield put({
                    type: 'setData',
                    payload: { allUsersList: [] }
                })
            }
            // const list = yield select(state=>state.users.list);
            // console.log(list) 
            // yield put({
            //   type: 'add'
            // });
        },
        *add({payload},{call}){
            // console.log(payload)
            return yield call(reportsService.add,payload);
        },
        * fetch({
            payload: {
                page,
            }
        }, {
            call,
            put,
            select,
        }) {
            console.log(page)
            let pageSize = yield select(state => state.reports.pageSize)
            const res = yield call(reportsService.fetchMyRpeorts, {
                page,
                pageSize,
            });
            console.log(res);
            if (res && res.state === 'success') {
                yield put({
                    type: 'setReports',
                    payload: {
                        ...res.data,
                        page
                    }
                });
            } else {
                yield put({
                    type: 'setReports',
                    payload: { list: { list: [], total: 0, page: 1, } }
                })
            }
            // const list = yield select(state=>state.users.list);
            // console.log(list) 
            // yield put({
            //   type: 'add'
            // });
        },
        *fetchInfo({payload},{call,put}){
            const res = yield call(reportsService.fetchInfo,payload);
            // console.log(res);
            if(res&&res.state==='success'){
                yield put({
                    type: 'setInfo',
                    payload: res.data,
                })
            }else{
                yield put({
                    type: 'setInfo',
                    payload: {},
                })
            }
        },
        *update({payload},{call}){
            return yield call(reportsService.update,payload);
        },
        *remove({payload},{call}){
            return yield call(reportsService.remove,payload);
        },
    },
    subscriptions: {
        setup({
            dispatch,
            history
        }) {
            return history.listen(({
                pathname
            }) => {
                if (pathname === '/reports') {
                    dispatch({
                        type: 'fetch',
                        payload: {
                            page: 1
                        }
                    });
                }
            })
        }
    }

}
